import { Serialize } from '@/common/decorator/serialize.decorator';
import { IdsDto } from '@/common/dto/ids.dto';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Req,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Request } from 'express';
import { CreateUserDto } from './dto/create-user.dto';
import { ResultUserConverterDto } from './dto/result-converter.dto';
import { SearchUserDto } from './dto/search-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UsersService } from './users.service';

@Controller('users')
@ApiTags('用户模块')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post()
  @ApiOperation({ summary: '创建用户' })
  @Serialize(ResultUserConverterDto, true, { groups: ['showSysRoles'] }) // 只查询 @Expose的,并显示 showSysRoles
  create(@Body() createUserDto: CreateUserDto) {
    return this.usersService.create(createUserDto);
  }

  @Get('page')
  @ApiOperation({ summary: '获取用户列表' })
  findAll(@Query() query: SearchUserDto) {
    return this.usersService.findAll(query);
  }

  @Get(':id/form')
  @ApiOperation({ summary: '获取用户表单' })
  @Serialize(ResultUserConverterDto, false) // 全查，不查询@Exclude的
  findOne(@Param('id') id: string) {
    return this.usersService.findOne(id);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新用户' })
  @Serialize(ResultUserConverterDto, true, { groups: ['showSysRoles'] })
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.usersService.update(id, updateUserDto);
  }

  @Delete(':ids')
  @ApiOperation({ summary: '删除用户' })
  remove(@Param() ids: IdsDto) {
    return this.usersService.remove(ids);
  }

  @Get('options')
  @ApiOperation({ summary: '获取用户选项' })
  getOptions() {
    return this.usersService.getOptions();
  }

  @Put(':id/password/reset')
  @ApiOperation({ summary: '重置密码' })
  resetPassword(@Param('id') id: string, @Query() query: { password: string }) {
    return this.usersService.resetPassword(id, query.password);
  }

  @Get('profile')
  @ApiOperation({ summary: '获取个人中心用户信息' })
  getProfile(@Req() req: Request) {
    return this.usersService.getProfile(req);
  }

  @Get('me')
  @ApiOperation({ summary: '获取个人中心用户信息' })
  whoami(@Req() req: Request) {
    return this.usersService.whoami(req);
  }
}
